package com.wangjiyuan.giftshuo.ui.fragment.listtop.other;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.wangjiyuan.giftshuo.R;
import com.wangjiyuan.giftshuo.base.BaseFragment;
import com.wangjiyuan.giftshuo.bean.listtopbean.commentbean.CommentBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.everydaybean.EveryDayBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.maybelikebean.MayBeLikeBean;
import com.wangjiyuan.giftshuo.bean.listtopbean.singleproductbean.SingleProductBean;
import com.wangjiyuan.giftshuo.ui.activity.listtop.DetailsItemActivity;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.adapter.DetailsImageViewPagerAdapter;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.adapter.DetailsRecyclerViewAdapter;
import com.wangjiyuan.giftshuo.ui.fragment.listtop.interfaces.MyItemListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * <p>
 * 单品Fragment界面
 */
public class SingleProductFragment extends BaseFragment
        implements ListTopContract.IListTopView, MyItemListener {

    /**
     * 当前界面的ViewPager图片展示及对应的点
     */
    private ViewPager vp_details_item;
    private List<View> viewList = new ArrayList<>();
    private DetailsImageViewPagerAdapter detailsImageViewPagerAdapter;
    private LinearLayout dotLayout;

    /**
     * 当前界面的推荐及可能喜欢的数据
     */
    private List<MayBeLikeBean.DataBean.RecommendItemsBean> recommendItemsBeanList;
    private List<MayBeLikeBean.DataBean.RecommendPostsBean> recommendPostsBeanList;

    /**
     * 当前界面ViewPager图片及下面单品的信息数据
     */
    private int id;
    private SingleProductBean.DataBean dataBean;

    /**
     * 当前界面除了ViewPager图片展示其他内容的滚动
     */
    private RecyclerView recyclerView;
    private DetailsRecyclerViewAdapter detailsRecyclerViewAdapter;
    private List<Object> objectList = new ArrayList<>();

    /**
     * 启动MVP，获取推荐及可能喜欢的数据
     */
    private ListTopContract.IListTopPresenter iListTopPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            id = (int) bundle.getSerializable("itemId");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_single_product, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        vp_details_item = (ViewPager) view.findViewById(R.id.vp_details_item);
        dotLayout = (LinearLayout) view.findViewById(R.id.ll_dot);
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_details_item);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        viewList.clear();
        objectList.clear();
        startMVP();

    }

    private void startMVP() {
        iListTopPresenter = new ListTopPresenter(this);
        iListTopPresenter.getSingleProductData(id);

    }

    private void initRecyclerView() {
        detailsRecyclerViewAdapter = new DetailsRecyclerViewAdapter(mContext, objectList, this);
        GridLayoutManager layoutManager = new GridLayoutManager(
                getActivity(),
                2);
        //合并第一行
        layoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (detailsRecyclerViewAdapter.getItemViewType(position) == DetailsRecyclerViewAdapter.HEADER
                        || detailsRecyclerViewAdapter.getItemViewType(position)
                        == DetailsRecyclerViewAdapter.NICK) {
                    return 2;
                } else {
                    return 1;
                }
            }
        });
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(detailsRecyclerViewAdapter);

    }

    private void initDotData() {
        for (int i = 0; i < dataBean.getImageUrls().size(); i++) {
            ImageView imageView = new ImageView(getActivity());
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(10, 10);
            if (i < dataBean.getImageUrls().size()) {
                layoutParams.rightMargin = 20;
                layoutParams.bottomMargin = 10;
            }
            if (i == 0) {
                imageView.setSelected(true);
            }
            imageView.setLayoutParams(layoutParams);
            imageView.setImageResource(R.drawable.dot_listtop_details);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            dotLayout.addView(imageView);
        }
    }

    private void initImageViewPager() {
        detailsImageViewPagerAdapter = new DetailsImageViewPagerAdapter(viewList);
        vp_details_item.setOffscreenPageLimit(10);
        vp_details_item.setAdapter(detailsImageViewPagerAdapter);

        vp_details_item.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }

            @Override
            public void onPageSelected(int position) {
                int itemCount = dotLayout.getChildCount();
                for (int i = 0; i < itemCount; i++) {
                    View view = dotLayout.getChildAt(i);
                    if (i == position) {
                        view.setSelected(true);
                    } else {
                        view.setSelected(false);
                    }
                }
            }
        });
    }

    private void initVpItemData() {
        for (int i = 0; i < dataBean.getImageUrls().size(); i++) {
            ImageView iv = new ImageView(getActivity());
            iv.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            viewList.add(iv);
            Glide.with(getActivity())
                    .load(dataBean.getImageUrls().get(i))
                    .placeholder(R.mipmap.post_place_holder_image)
                    .into((ImageView) viewList.get(i));
        }
    }

    @Override
    public void showProgress() {

    }

    @Override
    public void hideProgress() {

    }

    @Override
    public void showErrorInfo(String msg) {

    }

    @Override
    public void getEveryDayData(EveryDayBean everyDayBean) {

    }

    @Override
    public void getMayBeLikeData(MayBeLikeBean mayBeLikeBean) {
        recommendItemsBeanList = mayBeLikeBean.getData().getRecommendItems();
        recommendPostsBeanList = mayBeLikeBean.getData().getRecommendPosts();
        objectList.add(recommendPostsBeanList);
        objectList.addAll(recommendItemsBeanList);
        initRecyclerView();


    }

    @Override
    public void getCommentData(CommentBean commentBean) {
    }

    @Override
    public void getSingleProductData(SingleProductBean singleProductBean) {
        dataBean = singleProductBean.getData();
        objectList.add(dataBean);
        initDotData();
        initVpItemData();
        initImageViewPager();
        iListTopPresenter.getMayBeLikeData(id);
    }

    @Override
    public void getPosition(int position) {
        Intent intent = new Intent(getActivity(), DetailsItemActivity.class);
        intent.putExtra("itemId", recommendItemsBeanList.get(position-2).getId());
        startActivity(intent);
    }
}
